package sis.studentinfo;

import java.util.*;


/**
 * @author anyman
 *
 */
public class CourseSession {
	
	static final String NEWLINE = System.getProperty("line.separator");
	static final String ROSTER_REPORT_HEADER = "Student" + NEWLINE + "-" + NEWLINE;
	static final String ROSTER_REPORT_FOOTER = NEWLINE + "# students = ";
	
	private String department;
	private String number;
	private ArrayList<Student> students = new ArrayList<Student>();
	private Date startDate;
	private int numberOfCredits;
	static int count;

	public CourseSession(String department, String number) {
		this.department = department;
		this.number = number;
	}
	
	private CourseSession(String department, String number, Date startDate){
		this.department = department;
		this.number = number;
		this.startDate = startDate;		
	}
	
	public static CourseSession create(String department,String number, Date startDate){
		
		incrementCount();
		return new CourseSession(department, number, startDate);
	}	

	private static void incrementCount() {
		++ count;		
	}

	public String getDepartment() {
		
		return department;
	}
 
	public String getNumber() {
		
		return number;
	}	

	public int getNumberOfStudents() {
		
		return students.size();
	}

	public void enroll(Student student) {
		
		student.addCredits(numberOfCredits);
		students.add(student);
	}

	public ArrayList<Student> getAllStudents() {
		
		return students;
	}

	Student get(int index) {

		return students.get(index);
	}
	
	Date getStartDate() {

		return startDate;
	}	
	
	/**
	 * @return Date the last date of the course session
	 */
	Date getEndDate(){
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(startDate);

		// weeks * days per week - 3 days		
		final int sessionLength = 16;
		final int daysInWeek = 7;
		final int daysFromFridayToMonday = 3;
		int numbeOfDays = sessionLength * daysInWeek - daysFromFridayToMonday;
		calendar.add(Calendar.DAY_OF_YEAR, numbeOfDays);
		
		return calendar.getTime();
	}
	
	static void resetCount(){
		count = 0;
	}	
	
	static int getCount(){
		return count;
	}	

	 void setNumberOfCredits(int numberOfCredits) {

		 this.numberOfCredits = numberOfCredits;		 
	}		

	String getReport(){
		StringBuilder buffer = new StringBuilder();
		writeHeader(buffer);
		writeBody(buffer);
		writeFooter(buffer);
		
		return buffer.toString();
	}
	
	void writeHeader(StringBuilder buffer) {
		buffer.append(ROSTER_REPORT_HEADER);
	}
	
	void writeBody(StringBuilder buffer) {
		
		for(Student student: getAllStudents()) {
			buffer.append(student.getName());
			buffer.append(NEWLINE);		
		}
	}
	
	void writeFooter(StringBuilder buffer) {
		
		buffer.append(ROSTER_REPORT_FOOTER + getAllStudents().size() + NEWLINE);
	}


	/* String getRosterReport() {
		
		StringBuilder buffer = new StringBuilder();
		
		buffer.append(ROSTER_REPORT_HEADER);
		
		for(Student student: students) {
			buffer.append(student.getName());
			buffer.append(NEWLINE);		
		}
		
		buffer.append(ROSTER_REPORT_FOOTER + students.size() + NEWLINE);
		
		return buffer.toString();			
	}*/
}
